<?php
/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2018/4/15
 * Time: 下午11:16
 */

namespace App\Busi\Traits;




use App\Exceptions\ErrorCode\UserError;
use App\Exceptions\UserException;
use App\Models\Company;
use App\Models\Consult;
use App\Models\SecQualifyCategory;
use App\Models\SupQualifyCategory;

Trait ConsultTrait
{
    public function consultAdd($name,$address,$phone){
        $consult = new Consult();
        $consult->name = $name;
        $consult->address = $address;
        $consult->phone = $phone;
        $rs = $consult->saveOrFail();
        if($rs){
            $msg = "上传成功";
        }else{
            $msg = "上传失败";
        }
        $ret = [
            "msg" =>$msg
        ];
        return $ret;

    }
    public function getIndexConsultList(){
        $num = 5;
        $where = [
            ["status","=",1],
            ["flag","=",1]
        ];
        $fields = ['supcategory_id','subcategory_id','address','phone','created_at'];
        $data = Consult::where($where)->select($fields)->limit($num)->orderBy("created_at","desc")->get();
        $count = Consult::where($where)->count();
        $order_list = [];
        if($data){
            foreach ($data as $item){
                $tmp = [];
                $tmp['category_name'] = $this->getAllCategoryNameByID($item['subcategory_id']);
                $tmp['phone'] = substr_replace($item['phone'],'****',3,4);
                $tmp['address'] = $item['address'];
                $tmp['create_time'] = substr($item['created_at'],0,10);
                array_push($order_list,$tmp);
            }
        }
        $ret = [
            "total_orders"=>$count,
            "list"=>$order_list
        ];
        return $ret;
    }
    public function getConsultListByUid($uid,$page,$num = 5){
        $company = $this->getCompanyByUid($uid);
        if(!$company){
            throw new UserException(UserError::USER_NOT_AUTH);
        }
        if(!$company['flag'] == 1){
            throw new UserException(UserError::USER_AUTHING);
        }
        if(!$company['flag'] == 2){
            throw new UserException(UserError::USER_AUTH_PASS);
        }
        $company_id = $company['id'];
        $fields = ['id','supcategory_id','subcategory_id','address','phone','remark','is_view','created_at'];
        $where = [
            ['company_id','=',$company_id],
            ['flag','=',1]
        ];

        $data = Consult::where($where)->select($fields)->orderBy('created_at', 'asc')->paginate($num,$fields,$page,$page);
        $total = $data->total();
        $pageNum =  ceil($total/$num);
        $order_list = [];
        if($data){
            foreach ($data as $item){
                $tmp = [];
                $tmp['id'] = $item['id'];
                $tmp['category_name'] = $this->getAllCategoryNameByID($item['subcategory_id']);
                $tmp['phone'] = substr_replace($item['phone'],'****',3,4);
                $tmp['address'] = $item['address'];
                $tmp['create_time'] = substr($item['created_at'],0,10);
                $tmp['status'] = $item['is_view'] == 0? "未查看":"已查看";
                array_push($order_list,$tmp);
            }
        }
        $ret = [
            "total_orders"=>$total,
            "page_num"=>$pageNum,
            "list"=>$order_list
        ];
        return $ret;


    }
    function getCompanyByUid($uid)
    {
        $where = [
//            ['flag','=',3],
            ['uid','=',$uid]
        ];
        $company = Company::where($where)->first();
        return $company;
    }

    public function getAllCategoryNameByID($category_id){
        $where = [
            ["id","=",$category_id],
            ["flag","=",1]
        ];
        $fileds = ["id","category_name","parent_cate_id"];
        $data = SecQualifyCategory::where($where)->select($fileds)->first();
        if(!$data){
            return "";
        }
        $data = $data->toArray();
        $subcatename = $data['category_name'];
        $where_sup = [
            ["id","=",$data['parent_cate_id']],
            ["flag","=",1]
        ];
        $supcatename = SupQualifyCategory::where($where_sup)->select(["category_name"])->first();
        if($supcatename){
            $supcatename = $supcatename->toArray()["category_name"];
        }

        return $supcatename."-".$subcatename;



    }
    public function getTotalOrdersByUid($uid){
        $company = $this->getCompanyByUid($uid);
        if(!$company || $company['flag'] != 3){
            $ret = [
                'total'=>0,
                'used'=>0,
                'balance'=>0,
            ];
            return $ret;
        }


        $company_id = $company['id'];
        $where = [
            ['company_id','=',$company_id],
            ['flag','=',1]
        ];

        $data1 = Consult::where($where)->get();
        $total = count($data1);
        $where2 = [
            ['company_id','=',$company_id],
            ['is_view','=',1],
            ['flag','=',1],

        ];

        $data2 = Consult::where($where2)->get();
        $used = count($data2);
        $balance = $total - $used;
        $ret = [
            'total'=>$total,
            'used'=>$used,
            'balance'=>$balance,
        ];
        return $ret;
    }
}